package com.xiaohudie.config;

import ch.qos.logback.classic.db.DBAppender;
import ch.qos.logback.classic.db.DBHelper;
import com.xiaohudie.aop.GlobalInterception;
import com.xiaohudie.aop.LoginBlocker;
import com.xiaohudie.pojo.People;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.util.UrlPathHelper;

/**
 * @Author xiao hu die
 * @Date 2022/2/22 21:54
 * @Version 1.0
 */
/**标志这个类为配置类*/
/**默认为ture
 * 指定为false的时候就不是从容器中拿到的对象，而是自己创建的*/
/**导入一个组件控制,默认是全类名的名字*/
@Import({People.class})
@Configuration(proxyBeanMethods = false)
/**加载本地xml中的对象进入容器*/
//@ImportResource("classpath:beans.xml")
public class Myconfig implements WebMvcConfigurer {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    /**调用构造方法生成对象
     * 只能标注在方法上*/
    /**表示只有当容器中有这个组件的时候才加载
     * 就是以前spring中的一个标签*/
    @ConditionalOnBean(type = "DBHelper.class")
    @Bean
   public People  people(){
   return new People();
  }
  /**自定义 redisTemplate 的 key value 序列化模板*/
  @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
      /**创建Template*/
      RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
      /**设置连接工厂*/
      redisTemplate.setConnectionFactory(redisConnectionFactory);
      /**设置值(存对象使用)的序列化工具*/
      GenericJackson2JsonRedisSerializer jsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
      /**设置key 和  hashKey 为String序列化*/
      redisTemplate.setKeySerializer(RedisSerializer.string());
      redisTemplate.setHashKeySerializer(RedisSerializer.string());
      /**Value和HashValue采用JSON序列化(也就是值为对象但是key为String)*/
      redisTemplate.setValueSerializer(jsonRedisSerializer);
      redisTemplate.setHashValueSerializer(jsonRedisSerializer);
      return redisTemplate;
  }
   /**重写这个方法。将矩阵变量作为正常值传入*/
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        UrlPathHelper urlPathHelper = new UrlPathHelper();
        urlPathHelper.setRemoveSemicolonContent(false);
        WebMvcConfigurer.super.configurePathMatch(configurer);
    }
  /*  @Bean
    public WebMvcConfigurer webMvcConfigurer(){
        return new WebMvcConfigurer() {
            @Override
            public void configurePathMatch(PathMatchConfigurer configurer) {
             //   WebMvcConfigurer.super.configurePathMatch(configurer);
                UrlPathHelper urlPathHelper = new UrlPathHelper();
                urlPathHelper.setRemoveSemicolonContent(false);
                configurer.setUrlPathHelper(urlPathHelper);
            }
        };
    }*/
     /**配置拦截器
      * 放在其中的是不需要拦截的
      * 首页登录页都是不需要用户登录就可以访问的，当然登录也行*/
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginBlocker()).excludePathPatterns("/phone/login" ,
                "/phone/code",
                "/phone/frontPage",
                "/shanghu/**",
                "/miaosha/**",
                "/putfile/**",
                "/hello/**").order(2);
        /**控制下面的拦截器先执行*/
      registry.addInterceptor(new GlobalInterception(stringRedisTemplate)).addPathPatterns("/**").order(0);
    }
}
